package cs276.pe1.lucene;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;

import cs276.pe1.spell.TermFrequencySpellingCorrector;
import cs276.pe1.spell.SpellingCorrector;
import cs276.util.Constants;

/**
 * Interactive console for IMDB Search Engine.  This supports two spell checkers:
 * <ul>
 * <li>Term Frequency Spell Checker</li>
 * <li>Lucene Spell Checker</li>
 * </ul>
 * @author alecmgo@gmail.com
 */

public class IMDBInteractive {
  
  //Represents which spell checker to use.
  public static int spellCheckerState = 1;
  
  /**
   * Represents an interactive prompt that the user can use.
   */
  public static void main(String[] args) throws Exception {
    BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    String input = "";

    System.out.println("Welcome to the IMDB Search Engine.\nType 'quit' anytime to quit.\n");
    
    System.out.print("Initializing term frequency spell checker...");
    String imdbFile = Constants.PE1_DIR + "imdb-plots-20081003.list.gz";
    SpellingCorrector sc1 = new TermFrequencySpellingCorrector(imdbFile);
    System.out.println("Done");
    
    System.out.print("Initializing Lucene spell checker...");
    SpellingCorrector sc2 = new LuceneSpellChecker();
    System.out.println("Done");
    
    System.out.print("Initializing IMDB index...");
    IMDBSearcher searcher = new IMDBSearcher();
    System.out.println("Done");
    
    while (input.toLowerCase().indexOf("quit") == -1) {
      System.out.print("\nType a search query: ");
      
      try {
        input = reader.readLine();
        
        if(input.equals("0")) {
          System.out.println("Turning off spell check");
          spellCheckerState = 0;
          continue;
        } else if(input.equals("1")) {
          System.out.println("Switching to Term Frequency spell checker");
          spellCheckerState = 1;
          continue;
        } else if(input.equals("2")){
          System.out.println("Switching to Lucene spell checker");
          spellCheckerState = 2;
          continue;
        }
        
        if(spellCheckerState != 0) {
          String rawInput = input;
          String prefix = "";
          if(input.indexOf(":") != -1) {
            prefix = input.substring(0, input.indexOf(":")) + ":";
            rawInput = input.substring(input.indexOf(":") + 1);
          }
          
          List<String> suggestions;
          if(spellCheckerState == 1) {
            suggestions = sc1.corrections(rawInput);
          } else {
            suggestions = sc2.corrections(rawInput);
          }

          if(suggestions != null && !suggestions.get(0).equals(rawInput)) {
            System.out.print("Did you mean: " + suggestions.get(0) + "[Y/n]? ");
            String answer = reader.readLine();
            if(answer.length() == 0 || answer.toLowerCase().charAt(0) != 'n') {
              input = prefix + suggestions.get(0);
            }
          }
        }
        
        searcher.search(input);
        
      } catch (IOException e) {
        System.out.println("There was an error");
      }
    }
  }
}